package io.virtualapp.widgets;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by cheng
 * PackageName VirtualApp
 * 2018/8/2 11:50
 */

public class waveView2 extends View implements View.OnClickListener{
    private int mRippleWeight;//一个波的周期长度
    private  int mRippleCount;
    private int mScreenWidth;
    private int mScreenHeight;
    private int mCenterY;//需要展示的位置中线
    private int mOffset;
    private Path mPath;
    private Paint mPaint;
    private ValueAnimator mValueAnimatior;

    public waveView2(Context context) {
        super(context);
    }

    public waveView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);//如若需要下方带颜色可用此样式
        mPaint.setStrokeWidth(8);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
    }

    public waveView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mPath = new Path();
        mScreenHeight = getMeasuredHeight();
        mScreenWidth = getMeasuredWidth();
        mCenterY = mScreenWidth / 2;
        mRippleWeight = mScreenHeight / 2;
        mRippleCount = Math.round(mScreenWidth / mRippleWeight + 2);
        setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();//重置笔画
        mPath.moveTo(-mRippleWeight,mCenterY);//波走完重新的起点
        /**
         * 如若波上下不够大，仅需修改mCenterY + 400及mCenterY - 400，
         * 如若波左右不够大，请把mRippleWeight值变大
         */
        for (int i = 0;i <  mRippleCount;i++) {
            mPath.quadTo(-mRippleWeight * 3 / 4 + i * mRippleWeight + mOffset,mCenterY + 100,-mRippleWeight / 2 + i * mRippleWeight + mOffset,mCenterY);
            mPath.quadTo(-mRippleWeight / 4 + i * mRippleWeight + mOffset,mCenterY - 100,i * mRippleWeight + mOffset,mCenterY);
        }
        canvas.drawPath(mPath,mPaint);
    }


    @Override
    public void onClick(View v) {
        mValueAnimatior = ValueAnimator.ofInt(0, mRippleWeight);
        mValueAnimatior.setDuration(2000);
        mValueAnimatior.setInterpolator(new LinearInterpolator());
        mValueAnimatior.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimatior.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mOffset = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        mValueAnimatior.start();
    }
}
